import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hzy_project_template/assets/assets.dart';
import 'package:hzy_project_template/models/normal_tool_btn_model.dart';
import 'package:lottie/lottie.dart';

import '../config/config_index.dart';
import '../utils/getx_util_tool.dart';
import '../utils/screenutil_tools.dart';
import '../utils/utils.dart';

typedef ItemTapCallback = void Function(HzyNormalItemModel itemModel);

/// 创建加载动画
Widget configLoadWidget({
  Size? size,
}) {
  return SizedBox(
    width: size?.width ?? 100,
    height: size?.height ?? 100,
  );
}

// 创建通用关闭按钮
createNormalCloseBtnWidget({
  double? size = 24,
  Color? iconColor = const Color.fromRGBO(
    71,
    83,
    109,
    1,
  ),
  Function? onTapClose,
  bool isNeedBox = true,
  Size boxSize = const Size(40, 40),
  bool isPosition = true,
  bool isCanTap = true,
}) {
  Widget body = Icon(
    Icons.close,
    size: size,
    color: iconColor,
  );
  body = isNeedBox
      ? SizedBox(
          width: boxSize.width,
          height: boxSize.height,
          child: body,
        )
      : body;
  body = isCanTap
      ? InkWell(
          onTap: () {
            if (onTapClose != null) {
              onTapClose();
            } else {
              currentGoback();
            }
          },
          child: body,
        )
      : body;

  body = isPosition
      ? Positioned(
          right: 0,
          top: 0,
          child: body,
        )
      : body;

  return body;
}

// 创建设备网络图片
Widget creatDeviceNetWorkImage({
  required bool isNocarObu,
  required double width,
  required double height,
}) {
  return Image.network(
    isNocarObu ? '' : '',
    width: width,
    height: height,
  );
}

/// 配置带有hero动画的图片加载器
configHeroNetImageWidget({
  String? url,
  String? heroTag,
  double? width,
  double? height,
  int index = -1,
}) {
  Widget body = conFigNetWorkImage(
    url: url,
    fit: BoxFit.cover,
    width: width,
    height: height,
    loadradius: 15,
  );
  String tag = index != -1 ? "ImageHero$index" : (url ?? "tag");
  tag = (heroTag ?? '') + tag;
  body = Hero(
    tag: tag,
    child: body,
  );
  return body;
}

/// 创建通用Card 内边距 12.w
Widget createNormalCardWidget({
  required Widget body,
  double? paddingW,
  double? marginW,
  Color bgColor = Colors.white,
  Decoration? decoration,
  double radius = 12,
  EdgeInsetsGeometry? padding,
  EdgeInsetsGeometry? margin,
}) {
  return Container(
    decoration: decoration ??
        BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(
            radius.r,
          ),
        ),
    width: SizeMacro.screenWidth,
    padding: padding ??
        EdgeInsets.only(
          left: paddingW ?? 16.w,
          right: paddingW ?? 16.w,
          top: paddingW ?? 16.w,
        ),
    margin: margin ??
        EdgeInsets.only(
          left: marginW ?? 12.w,
          right: marginW ?? 12.w,
          top: marginW ?? 12.w,
        ),
    child: body,
  );
}

// 空数据动图
Widget createNoDataWidget() {
  return kIsWeb
      ? Container()
      : Lottie.asset(
          'assets/json/empty.json',
          width: SizeMacro.screenWidth / 1.8,
          height: 220,
          fit: BoxFit.contain,
          alignment: Alignment.center,
        );
}

/// 创建主题色阴影
BoxShadow configThemeShadow({
  double opacity = 0.8,
}) {
  return BoxShadow(
    color: HzyCommonColor().colthemes.withOpacity(
          opacity,
        ),
    offset: const Offset(0, 3),
    blurRadius: 10,
    spreadRadius: 0,
  );
}

/// 创建灰色阴影
BoxShadow configShadow({
  double opacity = 0.4,
}) {
  return BoxShadow(
    color: HzyCommonColor().colc3c3c3.withOpacity(opacity),
    offset: const Offset(0, 0),
    blurRadius: 10.0,
    spreadRadius: 0,
  );
}

/// 创建包含深色模式Item
Widget configNormalDarkItemWidget({
  required HzyNormalItemModel itemModel,
  int? index,
  int? allNum,
  double radius = 16,
  bool isAllRadius = true,
  bool isShowLine = true,
  ItemTapCallback? onOtherTap,
  ItemTapCallback? onTap,
}) {
  itemModel.leftMsgColor = HzyCommonColor().wbgblacktextcolor;
  if (itemModel.isHintWidget == false) {
    itemModel.backGroudColor = HzyCommonColor().whitebackgroundColor;
  } else {
    itemModel.backGroudColor =
        itemModel.backGroudColor ?? HzyCommonColor().backgroudgrey;
  }
  if (index == 0 && radius != 0) {
    itemModel.borderRadius = BorderRadius.vertical(
      top: Radius.circular(
        radius,
      ),
    );
  }
  itemModel.isShowLine = isShowLine;
  if (index != null && allNum != null && isAllRadius) {
    if (index == allNum - 1) {
      itemModel.isShowLine = false;
      if (radius != 0) {
        itemModel.borderRadius = BorderRadius.vertical(
          bottom: Radius.circular(
            radius,
          ),
        );
      }
    }
  }
  if (index == 0 && allNum == 1 && radius != 0) {
    itemModel.borderRadius = BorderRadius.circular(
      radius,
    );
  }
  itemModel.lineColor = HzyCommonColor().linecolor;
  Widget body = HzyNormalItemWidget(
    itemModel: itemModel,
    tapItemCallback: (itemModel, iex) {
      if (itemModel.tapType == 1 && itemModel.router != null) {
        if (onTap != null) {
          onTap(itemModel);
        } else {
          currentToPage(
            name: itemModel.router!,
            arguments: itemModel,
          );
        }
      } else {
        if (onOtherTap != null) {
          onOtherTap(itemModel);
        }
      }
    },
  );
  return body;
}

/// 创建通用勾选按钮
configNormalCheckBoxWidget({
  bool isSelcet = false,
  double? size,
  Color? color,
}) {
  IconData? icon = Icons.radio_button_unchecked;
  if (isSelcet) icon = Icons.check_circle_rounded;
  Widget body = Icon(
    icon,
    size: size,
    color: color,
  );
  return body;
}

createImageLoadWidget({
  double? width,
  double? height,
}) {
  Widget body = AnimatedTextKit(
    animatedTexts: [
      WavyAnimatedText('火之夜'),
    ],
    isRepeatingAnimation: true,
    onTap: () {},
  );

  body = Container(
    width: width,
    height: height,
    alignment: Alignment.center,
    decoration: BoxDecoration(
      color: HzyCommonColor().colefedf3,
    ),
    child: body,
  );
  body = body = DefaultTextStyle(
    style: const TextStyle(
      fontSize: 10.0,
    ),
    child: body,
  );
  return body;
}

/// 创建通用 ---
normamlTopLine() {
  Widget body = Container(
    width: 30,
    height: 4,
    decoration: BoxDecoration(
      color: const Color.fromRGBO(97, 95, 96, 1),
      borderRadius: BorderRadius.circular(
        4,
      ),
    ),
  );
  return body;
}

/// 创建通用按钮
configOnlyTextBtnWidget({
  required String text,
  Color textColor = Colors.white,
  Color? bgColor,
  double? width,
  double? height = 44,
  TextStyle? textStyle,
  TapCallback? tapCall,
  bool isCanTap = true,
}) {
  Widget body = HzyBtnWidget(
    bgColor: isCanTap
        ? bgColor ?? loginBlackColor
        : HzyCommonColor().disableBtnBackGroudColor,
    height: height,
    text: text,
    width: width,
    textStyle: textStyle ??
        HzyTextStyle.fontBold(
          size: 16.sp,
          color: isCanTap ? textColor : HzyCommonColor().disableBtnTitleColor,
        ),
    borderRadius: BorderRadius.circular(
      6.r,
    ),
    tapCall: isCanTap ? tapCall : null,
  );
  return body;
}

/*
 * 编辑工具栏
 */
Widget createEditBtnWidget({
  List<NormalToolBtnModel>? list,
  double height = 60,
  Widget? otherWidget,
  TapIndexCallback? onTap,
}) {
  list ??= [
    NormalToolBtnModel(
      name: NoteLauncIdConfig.rotate.tr,
      path: Assets.commRotate,
    ),
    NormalToolBtnModel(
      name: NoteLauncIdConfig.flip.tr,
      path: Assets.commFlip,
    ),
  ];
  Widget body = Row(
    mainAxisAlignment: MainAxisAlignment.start,
    children: [
      ...List.generate(
        list.length,
        (index) => HzyBtnWidget(
          btnLayoutType: BtnLayoutType.topImg,
          imageWidget: Image.asset(
            list![index].path,
            width: 24,
          ),
          height: null,
          titleWidget: Text(
            list[index].name,
            style: HzyTextStyle.fontMedium(
              size: 14,
              color: HzyNormalColorS.colffffff,
            ),
          ),
          padding: const EdgeInsets.only(
            right: 20,
          ),
          tapCall: () {
            onTap?.call(list?[index].type ?? index);
          },
        ),
      ),
      Container(
        width: 1,
        height: 30,
        decoration: const BoxDecoration(
          color: Color(
            0xff39383C,
          ),
        ),
      ),
      const SizedBox(
        width: 20,
      ),
      otherWidget ?? Container(),
    ],
  );

  body = Container(
    padding: const EdgeInsets.symmetric(
      horizontal: 20,
    ),
    height: height.w,
    child: body,
  );
  return body;
}

/*
 * 底部自定义导航栏
 */
Widget createBottomNavBarWidget({
  required String title,
  Function? tapCanner,
  Function? tapSure,
}) {
  return Container(
    margin: EdgeInsets.only(
      left: 10.w,
      right: 10.w,
      bottom: configSafeBottomHeight(),
    ),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        IconButton(
          onPressed: () {
            if (tapCanner != null) {
              tapCanner();
            } else {
              currentGoback();
            }
          },
          icon: const Icon(
            Icons.close,
            color: Colors.white,
          ),
        ),
        Text(
          title,
          style: HzyTextStyle.fontBold(
            size: 16.sp,
            color: Colors.white,
          ),
        ),
        IconButton(
          onPressed: () {
            if (tapSure != null) {
              tapSure();
            }
          },
          icon: const Icon(
            Icons.done,
            color: Colors.white,
          ),
        )
      ],
    ),
  );
}

/// 创建底部黑色带阴影布局
Widget createBoxShadowBottomNavBarWidget({
  required Widget child,
}) {
  return Container(
    padding: EdgeInsets.only(
      top: 10.w,
    ),
    decoration: BoxDecoration(
      color: const Color(0xff201e22),
      boxShadow: [
        BoxShadow(
          color: HzyNormalColorS.colbfbfbf.withOpacity(0.3),
          offset: const Offset(0, 0),
          blurRadius: 10.0,
          spreadRadius: 0,
        )
      ],
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(20.r),
        topRight: Radius.circular(20.r),
      ),
    ),
    child: child,
  );
}

/// 创建底部NavBarWidget
createBoxShaowBottomNavBarWidget({
  required Widget child,
  Color? color = const Color(0xff201e22),
  BoxDecoration? decoration,
  EdgeInsetsGeometry? padding = const EdgeInsets.symmetric(vertical: 10),
  EdgeInsetsGeometry? margin,
}) {
  return Container(
    padding: padding,
    margin: margin,
    decoration: decoration ??
        BoxDecoration(
          color: color,
          boxShadow: [
            BoxShadow(
              color: HzyNormalColorS.colbfbfbf.withOpacity(0.6),
              offset: const Offset(0, 0),
              blurRadius: 10.0,
              spreadRadius: 0,
            )
          ],
          borderRadius: BorderRadius.circular(10.r),
        ),
    child: child,
  );
}
